package com.zxn.drag.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;

import com.zxn.drag.R;
import com.zxn.drag.callback.SimpleAnimationListener;
import com.zxn.drag.utils.AnimUtils;
import com.zxn.drag.utils.ShakeUtil;

/**
 * 删除区域布局.
 * Created by zxn on 2020/3/18.
 */
public class DeleteAreaLayout extends RelativeLayout {

    AppCompatImageView ivDeleteIcon;
    TextView tvDel;
    LinearLayout llDeleteIcon;
    RelativeLayout rlDeleteArea;
    private AnimationSet mShowAnimation;
    private AnimationSet mHideAnimation;
    private AnimationSet mFinishAnimation;
    private AnimationSet mHideTranslateAnim;

    public DeleteAreaLayout(Context context) {
        super(context);
        onInit();
    }


    public DeleteAreaLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        onInit();
    }

    public DeleteAreaLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        onInit();
    }

    private void onInit() {
        LayoutInflater.from(getContext())
                .inflate(R.layout.layout_delete_area, this);
        ivDeleteIcon = findViewById(R.id.iv_delete_icon);
        tvDel = findViewById(R.id.tv_del);
        llDeleteIcon = findViewById(R.id.ll_delete_icon);
        rlDeleteArea = findViewById(R.id.rl_delete_area);
        onInitAnim();
    }

    public void showDragStartView() {
        ivDeleteIcon.setImageResource(R.drawable.ic_edit_delete);
        tvDel.setText("删除");
        llDeleteIcon.setVisibility(VISIBLE);
        ivDeleteIcon.setVisibility(VISIBLE);
        tvDel.setVisibility(VISIBLE);
        setBackgroundColor(getResources().getColor(R.color.c_fb6b59));
        clearAnimation();
        setVisibility(View.VISIBLE);
        startAnimation(mShowAnimation);
    }

    public void showDragFinishView() {
        ivDeleteIcon.setImageResource(R.drawable.ic_delete_finish);
        tvDel.setText("完成");
        setBackgroundColor(getResources().getColor(R.color.c_0074FF));
        ivDeleteIcon.clearAnimation();
        ivDeleteIcon.startAnimation(mFinishAnimation);
    }

    public void showDragAreaChangeView(boolean isInside) {
        if (isInside) {
            ivDeleteIcon.setImageResource(R.drawable.ic_edit_deleted);
            setBackgroundColor(getResources().getColor(R.color.c_fb6b59));
            ShakeUtil.vibrator(getContext(), 100);
            //mRvDeleteArea.startAnimation(mDelShowScaleAnim);
        } else {
            ivDeleteIcon.setImageResource(R.drawable.ic_edit_delete);
            setBackgroundColor(getResources().getColor(R.color.c_fb6b59));
            //mRvDeleteArea.startAnimation(mDelHideScaleAnim);
        }
    }

    private void onInitAnim() {
        mShowAnimation = AnimUtils.createShowAnimation();
        mHideAnimation = AnimUtils.createHideAnimation(new SimpleAnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.INVISIBLE);
            }
        });

        mFinishAnimation = AnimUtils.createFinishAnimation(new SimpleAnimationListener(){
            @Override
            public void onAnimationEnd(Animation animation) {
                startAnimation(mHideAnimation);
            }
        });

        mHideTranslateAnim = AnimUtils.createHideTranslate(new SimpleAnimationListener(){
            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(View.INVISIBLE);
            }
        });
        /*mDelHideScaleAnim = new ScaleAnimation(1.3f, 1.0f, 1.3f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        mDelHideScaleAnim.setDuration(150);*/

        /*adapter.setEmptyAnimatorListener(new PicMgrAdapter.EmptyAnimatorListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation, PicMgrAdapter.PicAddViewHolder holder) {
                float value = (float) animation.getAnimatedValue();
                int color = ColorUtils.setAlphaComponent(0xffffffff, (int) (255 * value));
                holder.itemView.setBackgroundColor(color);
            }
        });*/

        /*btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.startEmptyAnimator(mRecyclerView, 1000L, 0.1f, 0.2f, 0.1f, 0.2f, 0.1f);
            }
        });*/
    }

    public void hideView() {
        clearAnimation();
        startAnimation(mHideAnimation);
    }

}
